package com.moli.lumpSugar.user.event.listener;


import com.moli.lumpSugar.common.cache.user.UserCache;
import com.moli.lumpSugar.user.dao.UserDao;
import com.moli.lumpSugar.user.domain.entity.User;
import com.moli.lumpSugar.user.event.BlackUserEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @program: LumpSugarChat
 * @description拉黑用户事件监听者
 * @author: zhangjt
 * @create: 2024-10-25 17:21
 **/
@Component
public class BlackUserEventListener {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserCache userCache;

    @Async
    @TransactionalEventListener(classes = BlackUserEvent.class, phase = TransactionPhase.AFTER_COMMIT)
    public void sendMsg(BlackUserEvent event) {
        // 通知所有在线用户拉黑的用户
        User blackUser = event.getUser();
//        webSocketService.sendAllOnlineUser(WSAdapter.buildBlackUserMsg(blackUser));
    }

    @Async
    @TransactionalEventListener(classes = BlackUserEvent.class, phase = TransactionPhase.AFTER_COMMIT)
    public void changeUserStatus(BlackUserEvent event) {
        // 拉黑用户，修改用户的使用状态
        userDao.invalidUid(event.getUser().getId());
    }

    @Async
    @TransactionalEventListener(classes = BlackUserEvent.class, phase = TransactionPhase.AFTER_COMMIT)
    public void evictCache(BlackUserEvent event) {
        // 清除用户拉黑缓存
        userCache.clearBlackList();
    }
}
